package top.soft.windows;

import top.soft.bean.User;
import top.soft.util.CommonData;
import top.soft.util.NetWork;
import top.soft.util.Request;
import top.soft.util.Response;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Set;

/**
 * @ClassName: AddView
 * @Author: Amosen
 * @Since: 2021/4/21
 */
// 这个类用于新增选项卡，给予新增用户的功能，这个页面仅有管理员有权限进入
public class AddView extends JPanel {

    private JLabel account, auth;
    private JTextField accountBar;
    private JComboBox<String> authSelection;
    private JButton confirmBtn, resetBtn;

    public AddView() {
        init();
    }

    private void init() {
        // 准备组件
        // 表单填写组件
        account = new JLabel(CommonData.ADD_PROPERTY_ACCOUNT);
        auth = new JLabel(CommonData.ADD_PROPERTY_AUTH);
        accountBar = new JTextField(15);
        Set<String> keys = CommonData.QUALIFICATION_MAP.keySet();
        String[] items = keys.toArray(new String[0]);
        authSelection = new JComboBox<String>(items);

        // 按钮组件
        confirmBtn = new JButton(CommonData.CONFIRM_BTN);
        resetBtn = new JButton(CommonData.RESET_BTN);
        confirmBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doAdd();
            }
        });
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doReset();
            }
        });

        // 将组件添加到面板中
        setLayout(new BorderLayout());
        JPanel colOne = new JPanel();
        JPanel colTwo = new JPanel();
        colOne.setLayout(new FlowLayout());
        colTwo.setLayout(new FlowLayout());
        colOne.add(account);
        colOne.add(accountBar);
        colTwo.add(auth);
        colTwo.add(authSelection);
        JPanel panelCenter = new JPanel();
        panelCenter.setLayout(new GridLayout(2, 2));
        panelCenter.add(colOne);
        panelCenter.add(colTwo);
        add(panelCenter, BorderLayout.CENTER);

        JPanel panelSouth = new JPanel();
        panelSouth.setLayout(new FlowLayout());
        panelSouth.add(confirmBtn);
        panelSouth.add(resetBtn);
        add(panelSouth, BorderLayout.SOUTH);

    }

    private boolean check() {
        String account = accountBar.getText();
        if (account == null) {
            accountBar.requestFocus();
            return false;
        } else {
            if ("".equals(account) || account.trim().length() == 0) {
                accountBar.requestFocus();
                return false;
            } else {
                return true;
            }
        }
    }

    // 添加成员
    private void doAdd() {
        // FIXME: 完成视图设计之后写这里
        User user = CommonData.getUser();
        if (user == null) {
            JOptionPane.showMessageDialog(null, "您还没有登录！");
            return;
        }
        String loginAccount = user.getAccount();
        String account = accountBar.getText();
        String strAuth = authSelection.getItemAt(authSelection.getSelectedIndex());
        Integer intAuth = CommonData.QUALIFICATION_MAP.get(strAuth);
        // 封装为Request对象
        Request request = new Request(CommonData.ADD_USER_PATH, Request.GET);
        request.addData("loginAccount", loginAccount)
                .addData("account", account)
                .addData("auth", String.valueOf(intAuth));
        Response response = NetWork.service(request);
        if (response == null) {
            JOptionPane.showMessageDialog(null, "网络连接失败！");
        } else {
            if (response.getCode() == 504) {
                JOptionPane.showMessageDialog(null, "您没有权限！");
            } else if (response.getCode() == 502) {
                JOptionPane.showMessageDialog(null, "该账号已存在！");
            } else {
                JOptionPane.showMessageDialog(null, "添加成功，密码默认与账户名一致！");
                doReset();
            }
        }
    }

    // 重置表单组件
    private void doReset() {
        //FIXME: 完成视图设计之后写这里
        accountBar.setText("");
    }

}
